#!/bin/bash

RUN_TYPE=$1
pid=`jps -l | grep org.elasticsearch.bootstrap.Elasticsearch | awk '{print $1}'`

if [ -z "$RUN_TYPE" ]; then
  echo "ERROR only one params at least [start | stop | status]"
    exit 1
elif [ "$RUN_TYPE" == "start" ]; then
    if [ -n "$pid" ] ; then
        echo "ERROR The elasticsearch server(${pid}) already running."
        exit 1;
    fi
    RUN_CMD=nohup ./bin/elasticsearch > start.out 2>&1 &
    $RUN_CMD
    echo "The elasticsearch server will starting..."
elif [ "$RUN_TYPE" == "stop" ]; then
  if [ -z "$pid" ] ; then
        echo "ERROR No elasticsearch server running."
        exit 1;
    fi
    echo "The elasticsearch server(${pid}) is running..."
    RUN_CMD="kill ${pid}"
    $RUN_CMD
    echo "Send shutdown request to elasticsearch server(${pid}) OK"
elif [ "$RUN_TYPE" == "status" ]; then
    if [ -z "$pid" ] ; then
        echo "No elasticsearch server running."
    else
        echo "The elasticsearch server('${pid}') already running."
    fi
else
     echo "ERROR Error unknown params"
     exit 1
fi